package com.cuanbobox.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import com.cuanbobox.base.App;
import com.cuanbobox.cbavcx.R;

/**
 * 纯色进度条
 */
public class ColorSeekBar extends View {
    private float width;
    private float height;
    private float barHeight;
    private float thumbWH;
    private int maxProgress = 100;
    private int nowProgress = 0;
    private float unit;
    private float barTopOffset;
    private float barLeft;
    private float barRight;
    private Paint bgBarPaint;
    private Paint barPaint;
    private Paint thumbPaint;
    private float barRadius;
    private int bgBarColor;
    private int barColor;
    private int thumbColor;
    private float thumbY;
    private float thumbRadius;
    private OnTouchDataChangeListener mOnTouchDataChangeListener;
    private boolean allowTouch=true;

    public ColorSeekBar(Context context) {
        this(context, null);
    }

    public ColorSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public ColorSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.colorseekbar, defStyleAttr, 0);
        try {
            //getLayoutDimension 返回-2代表wrap_content 返回-1代表 match_parents
            barHeight = a.getDimension(R.styleable.colorseekbar_barHeight, 0);
            thumbWH = a.getDimension(R.styleable.colorseekbar_thumbWH, 0);
            barRadius = a.getDimension(R.styleable.colorseekbar_barRadius, 0);
            bgBarColor = a.getColor(R.styleable.colorseekbar_bgBarColor, 0);
            barColor = a.getColor(R.styleable.colorseekbar_barColor, 0);
            thumbColor = a.getColor(R.styleable.colorseekbar_thumbColor, 0);
        } finally {
            a.recycle();
        }
        init();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        width = w;
        height = h;
        unit = (width - thumbWH) / maxProgress;
        barTopOffset = (height - barHeight) / 2f;
        thumbY = height / 2f;
        barLeft = thumbWH / 2f;
        barRight = width - thumbWH / 2f;
        thumbRadius = thumbWH / 2f;
    }

    private void init() {
        bgBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bgBarPaint.setColor(bgBarColor);
        barPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        barPaint.setColor(barColor);
        thumbPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        thumbPaint.setColor(thumbColor);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画背景条
        canvas.drawRoundRect(barLeft, barTopOffset, barRight, barTopOffset + barHeight, barRadius, barRadius, bgBarPaint);
        //画前景条
        canvas.drawRoundRect(barLeft, barTopOffset, nowProgress * unit + barLeft, barTopOffset + barHeight, barRadius, barRadius, barPaint);
        //画圆
        canvas.drawCircle(nowProgress * unit + thumbWH / 2f, thumbY, thumbRadius, thumbPaint);
    }

    public void setAllowTouch(boolean allowTouch) {
        this.allowTouch = allowTouch;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!allowTouch) return true;
        float x = event.getX();
        if (x < barLeft) {
            nowProgress = 0;
        } else if (x > barRight - barLeft) {
            nowProgress = maxProgress;
        } else {
            nowProgress = (int) (x / unit);
        }
        if (mOnTouchDataChangeListener != null) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                mOnTouchDataChangeListener.onChange(nowProgress, true);
            } else {
                mOnTouchDataChangeListener.onChange(nowProgress, false);
            }

        }
        invalidate();
        return true;
    }

    public void setOnTouchDataChangeListener(OnTouchDataChangeListener mOnTouchDataChangeListener) {
        this.mOnTouchDataChangeListener = mOnTouchDataChangeListener;
    }

    public void setMaxProgress(int maxProgress) {
        this.maxProgress = maxProgress;
        unit = (width - thumbWH) / maxProgress;
    }

    public void setNowProgress(int nowProgress) {
        this.nowProgress = nowProgress;
        invalidate();
    }

    public int getNowProgress() {
        return nowProgress;
    }

    public interface OnTouchDataChangeListener {
        void onChange(int nowProgress, boolean isActionUp);
    }
}
